#include "view_blood_glucose.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "times/view_time_0.h"
#include "sports_mode.h"
#include "app_trace.h"
#include "view_blood_glucose.inc"
#define MEASURE_SECOND 30
// #include "Heart_rate_additional.h"
#define LOG(fmt, ...) //app_trace_log(fmt "\r\n", ##__VA_ARGS__)

void view_blood_glucose_animation(view_blood_glucose_param const *param)
{
    if (param == 0 || param->file_index_0 < DISP_LFS_FILE_INDEX_BEGIN || param->file_index_0 >= DISP_LFS_FILE_INDEX_END || param->file_index_1 < DISP_LFS_FILE_INDEX_BEGIN ||
        param->file_index_1 >= DISP_LFS_FILE_INDEX_END)
    {
        return;
    }

    if (!param->is_wear)
    {
        _disp_show_flash_img(param->file_index_0, 25, 73, FILE_IMG_J115_notwearingawatch_25_73);
        disp_show_localize((disp_little_fs_file_index_t)param->file_index_1, localize_string_key_AlertNotWear, param->temp_index);
        return;
    }
    // int x = 0;
    // int y = 0;

    _disp_show_flash_img(param->file_index_0, 51, 19, FILE_IMG_icon_51_19);

    int const font_width = 16;
    int const num_interval = 6;
    int const num_width = font_width + num_interval;
    int const num_height = 37;

    int const point_interval = 6;
    int const point_width = 6;
    int const point_height = 8;

    uint8_t const *const second_img = img_common_second;
    int const second_width = 21;
    int const second_height = 19;
    int const second_interval = 6;

    uint8_t const *const unit_img = icon_mmol_l;
    int const unit_width = 63;
    int const unit_height = 18;
    int const unit_interval = 6;

    int y = 187;
    if (param->seconds >= 0)
    {
        uint8_t digits = get_num_digit(param->seconds);
        uint32_t width_total = digits * num_width - num_interval + second_width + second_interval;
        int x = (240 - width_total) / 2;
        x = disp_draw_user_image_num(x, y, param->seconds, num_width, img_j501_TAB) - num_interval;
        x += second_interval;
        disp_draw_user_image(x, y + num_height - second_height, second_img);
    }
    else
    {
        if (param->value < 0)
        {
            return;
            // param->value = 0;
        }
        int x = 0;

        int const interger_part = param->value / 100;
        int const fractional_part = param->value % 100;
        int const fractional_digits = 2;
        int const interger_digits = get_num_digit(interger_part);

        uint32_t width_total = interger_digits * num_width - num_interval + point_height + 2 * point_interval + fractional_digits * num_width - num_interval + unit_width + unit_interval;
        x = (240 - width_total) / 2;

        x = disp_draw_user_image_num(x, y, interger_part, num_width, img_j501_TAB) - num_interval;
        x += point_interval;
        disp_draw_rect_color(x, y + num_height - point_height, x + point_width - 1, y + num_height - 1, RGB565_COLOR_WHITE);
        x += point_width + point_interval;
        x = disp_draw_user_image_num_with_digits(x, y, fractional_part, 2, num_width, img_j501_TAB) - num_interval;

        x += unit_interval;
        disp_draw_user_image(x, y + num_height - unit_height, unit_img);
    }
}

void view_blood_glucose_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.i_temp[0] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();

        // 血糖：9:00 - 12:00, 14:00 - 17:00, 20:00 - 22:00    =>.    这些时间范围内随机  4.5 - 5
        // 8:00 - 9:00，12:00 - 14:00， 17:00 - 20:00  =>  这些时间范围内随机   6 -   7
        // 22:00 - 次日 8:00 =>  这些时间范围内随机  3.5 - 4

        if (disp_ctrl.auto_refresh_count == MEASURE_SECOND * 2)
        {
            int minimum = 0;
            int maximum = 0;
            if (UIdatetime.time.hour <= 8 || UIdatetime.time.hour >= 22)
            {
                minimum = 350;
                maximum = 400;
            }
            else if ((UIdatetime.time.hour >= 8 && UIdatetime.time.hour <= 9) || (UIdatetime.time.hour >= 12 && UIdatetime.time.hour <= 14) ||
                     (UIdatetime.time.hour >= 17 && UIdatetime.time.hour <= 20))
            {
                minimum = 450;
                maximum = 500;
            }
            else
            {
                minimum = 450;
                maximum = 500;
            }
            int range = maximum - minimum + 1;
            int random = rand() % range;
            disp_ctrl.com_data.i_temp[10] = minimum + random;
            LOG("min:%d max:%d random:%d value:%d", minimum, maximum, random, disp_ctrl.com_data.i_temp[10]);
        }
    }
    if (disp_ctrl.com_data.i_temp[5])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }
    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    int num = disp_ctrl.com_data.i_temp[0];
    if (num == 0)
    {
        num = -1;
    }
    if (disp_ctrl.auto_refresh_count < MEASURE_SECOND * 2)
    {
        view_blood_glucose_param const param = {
            .is_wear = disp_ctrl.com_data.i_temp[1],
            .seconds = (MEASURE_SECOND * 2 + 1 - disp_ctrl.auto_refresh_count) / 2,
            .file_index_0 = DISP_LFS_FILE_INDEX_0,
            .file_index_1 = DISP_LFS_FILE_INDEX_1,
            .temp_index = 50,
        };
        view_blood_glucose_animation(&param);
    }
    else
    {
        view_blood_glucose_param const param = {
            .is_wear = true,
            .seconds = -1,
            .value = disp_ctrl.com_data.i_temp[10],
            .file_index_0 = DISP_LFS_FILE_INDEX_0,
            .file_index_1 = DISP_LFS_FILE_INDEX_1,
            .temp_index = 50,
        };
        view_blood_glucose_animation(&param);
    }
}
view_event_result_t view_blood_glucose_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    case event_view_entry:
        disp_ctrl.auto_close_delay = (MEASURE_SECOND + 10) * 1000;
        hr_test_start(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    case event_view_re_screen_on:
        disp_ctrl.auto_close_delay = (MEASURE_SECOND + 10) * 1000;
        return view_event_result_handled;
    case event_view_exit:
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
